package apig.client.prof;

import java.util.*;

import org.gwm.client.*;
import org.gwm.client.impl.*;

import apig.client.*;
import apig.client.serviface.*;

import com.google.gwt.core.client.*;
import com.google.gwt.user.client.*;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.rpc.*;
import com.google.gwt.user.client.ui.*;

public class ProfEntryPoint implements EntryPoint {

	private UserData ud;
	private ProfHeader header;
	private GDesktopPane gdp;
	private MenuBar studBar;
	private CodeBrowseHandler cbh = new CodeBrowseHandler();
	
	public void onModuleLoad() {

		Stubman.getStubman().getService()
				.getUserData(new AsyncCallback<UserData>() {

					@Override
					public void onFailure(Throwable caught) {
						onGetDataFailed(caught.getMessage());
					}

					@Override
					public void onSuccess(UserData result) {
						onGetDataSuccess(result);
					}
				});
	}

	private void onLoadUi() {

		RootPanel rlp = RootPanel.get("container");
		VerticalPanel vp = new VerticalPanel();
		gdp = new DefaultGDesktopPane();
		vp.setWidth("100%");
		vp.setHeight("100%");
		vp.setBorderWidth(5);
		header = new ProfHeader(ud);
		vp.add(header);
		MenuBar bar = createMenuBar();
		vp.add(bar);
		vp.add((Widget) gdp);
		bar.setWidth("100%");
		vp.setCellWidth(bar, "100%");
		vp.setCellWidth((Widget) gdp, "100%");
		vp.setCellHeight((Widget) gdp, "100%");
		rlp.add(vp);

	}

	private MenuBar createMenuBar() {
		MenuBar bar = new MenuBar(false);
		MenuBar vBar = new MenuBar();
		bar.addItem("Меню", vBar);
		Command alertCommand = new Command() {
			@Override
			public void execute() {
				onAlertCommand();
			}
		};
		Command userManCommand = new Command() {
			@Override
			public void execute() {
				onUserManCommand();
			}
		};

		studBar = new MenuBar();
		vBar.addItem("Студенты", studBar);
		vBar.addItem(new MenuItem("Учетные записи", userManCommand));
		vBar.addItem(new MenuItem("Алерты", alertCommand));

		reloadStudMenu();
		Timer t = new Timer() {
			public void run() {
				reloadStudMenu();
				schedule(Stubman.TIMER_DELAY);
			}
		};
		t.schedule(Stubman.TIMER_DELAY);
		return bar;
	}

	private void reloadStudMenu() {
		Stubman.getStubman().getService().getAllStuds(
				new AsyncCallback<HashMap<Group, List<Student>>>(){

					@Override
					public void onFailure(Throwable caught) {
						System.out.println("fail to load studs: " + caught.getMessage());
					}

					@Override
					public void onSuccess(HashMap<Group, List<Student>> result) {
						onStudListLoad(result);
					}});
	}
	
	private void onStudListLoad(HashMap<Group, List<Student>> result) {
		studBar.clearItems();
		for (Group g : result.keySet()) {
			MenuBar gBar = new MenuBar(true);
			studBar.addItem(g.name, gBar);
			for (Student s : result.get(g)) {
				gBar.addItem(new MenuItem(s.name + " " + s.patr, new StudCommand(s)));
			}
		}
	}
	
	private void onBrowseCode(Code c) {
		if (c.hypoCount == 0) {
			return;
		}
		CodeInternalFrame cif = new CodeInternalFrame(c);
		cif.setSize(800, 500);
		gdp.addFrame(cif);
		cif.setResizable(true);
		cif.setVisible(true);
	}

	private void onUserManCommand() {
		
	}

	private void onAlertCommand() {

	}

	private void onStudCommand(Student s) {
		StudInternalFrame sif = new StudInternalFrame(s, cbh);
		sif.setSize(800, 500);
		gdp.addFrame(sif);
		sif.setResizable(true);
		sif.setVisible(true);
	}

	private void onGetDataSuccess(UserData result) {
		if (result != null && result.logged
				&& result.role == UserData.ROLE_PROF) {
			ud = result;
			onLoadUi();
		} else {
			System.out.println("unauthorized");
		}
	}

	private void onGetDataFailed(String msg) {
		System.out.println("fail: " + msg);
	}
	
	private class CodeBrowseHandler implements CodeBrowseListener {
		@Override
		public void browseCode(Code c) {
			onBrowseCode(c);
		}		
	}
	
	private class StudCommand implements Command {

		private Student s;

		public StudCommand(Student s) {
			this.s = s;
		}

		@Override
		public void execute() {
			onStudCommand(s);
		}
	}
}
